/**
 * Project Name:youba
 * Package Name:com.inoneone.youba.mcht.service.impl
 * Date:2017/7/6下午12:13
 * Copyright (c) 2017, www.zbj.com All Rights Reserved
 */
package com.inoneone.youba.mcht.service.impl;

import com.github.pagehelper.PageHelper;
import com.inoneone.youba.mcht.dao.mapper.HotUpdateMapper;
import com.inoneone.youba.mcht.dao.po.HotUpdate;
import com.inoneone.youba.mcht.dao.po.HotUpdateExample;
import com.inoneone.youba.mcht.dto.rsp.GetLeastVersionRsp;
import com.inoneone.youba.mcht.enums.ErrorCode;
import com.inoneone.toolkit.exception.BusinessRuntimeException;
import com.inoneone.youba.mcht.service.HotUpdateApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description: (描述) </br>
 * Date: 2017/7/6下午12:13 </br>
 *
 * @author 姬远玄
 * @since JDK 1.7
 */
@Service
public class HotUpdateApiImpl implements HotUpdateApi {

    @Autowired
    HotUpdateMapper hotUpdateMapper;

    /**
     * Description: 查询最新版本数据</br>
     * Date: 2017-07-06 12:28:50</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
    */
    @Override
    public GetLeastVersionRsp getLeastVersion()
    {
        HotUpdateExample hotUpdateExample = new HotUpdateExample();
        hotUpdateExample.setOrderByClause("v1 desc, v2 desc, v2 desc");
        PageHelper.startPage(1, 1);

        List<HotUpdate> hotUpdateList = hotUpdateMapper.selectByExample(hotUpdateExample);
        if(hotUpdateList.size() == 0)
        {
            throw new BusinessRuntimeException(ErrorCode.ORDER_NOT_EXIST);
        }

        HotUpdate leastHotUpdate = hotUpdateList.get(0);

        GetLeastVersionRsp getLeastVersionRsp = new GetLeastVersionRsp();
        getLeastVersionRsp.setLeastVersion(String.format("%d.%d.%d", leastHotUpdate.getV1(), leastHotUpdate.getV2(), leastHotUpdate.getV3()));

        return getLeastVersionRsp;
    }
}
